package cn.com.fssg.platform.base.handler.impl.cl.org;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ClOrgClockInListInterfaceHandler implements InterfaceHandler {

//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {

        List<Map<String, Object>> list = new ArrayList<>();
        //判断是否为机构，后面导入正式数据判断的角色名也要跟着改
        boolean isGg = BizUtil.cur().curUserRoleContain("organization_user");
//        boolean isGg = true;
        String month = jsonObject.getString("month");
        if(isGg) {
//        if(true) {
            //机构登录，查询机构下所有人员信息
            String orgId = BizUtil.cur().curUserDeptId();
//            String orgId = "8de0cc59a02abebb1c1642453a0078d0";
//            String orgId = "1981368458152419329";
            String querySql = getQuerySql(month);
            String dyqId = jsonObject.getString("dyqId");
            Map<String, Object> params = new HashMap<>();
            params.put("org_id", orgId);
            if (!ObjectUtils.isEmpty(dyqId)) {
                querySql += " and b.id =:dyqId";
                params.put("dyqId", dyqId);
            }
            list = jdbcTemplate.queryForList(querySql, params);
        }
        /*boolean isUser = BizUtil.cur().curUserRoleContain("regular_user");
        if(isUser) {
            //人员登录，查询机构下所有人员信息
//            editUserId = jsonObject.getString("zw_id");
        }*/
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            //获取人员签到信息
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", map.get("id"));//代金券ID
            resultMap.put("name", map.get("cjr_name"));//人员姓名
            resultMap.put("status", map.get("status"));//签到类别
            resultMap.put("initial", getPinyinInitials((String) map.get("cjr_name")));
            resultMap.put("cjrServiceType", map.get("cjr_service_type"));
            resultMap.put("cjrServiceCat", map.get("cjr_service_cat"));
            resultList.add(resultMap);
        }
        return resultList;
    }

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response, JSONObject jsonObject) throws Exception {
        InterfaceHandler.super.run(request, response, jsonObject);
    }

    public static String getPinyinInitials(String name) {
        if (name == null || name.isEmpty()) {
            return "";
        }
        // 只处理第一个字符
        char firstChar = name.charAt(0);
        if (Character.toString(firstChar).matches("[\\u4E00-\\u9FA5]")) {
            // 中文字符
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(firstChar);
            if (pinyinArray != null && pinyinArray.length > 0) {
                return pinyinArray[0].substring(0, 1).toUpperCase();
            }
        } else {
            // 非中文字符
            return Character.toString(Character.toUpperCase(firstChar));
        }
        return "";
    }
    private String getQuerySql(String month) {
        String sql, condition;
        String preSql = "SELECT " +
                "    b.cjr_name," +
                "    CASE " +
                "        WHEN a.sign_in_time IS NOT NULL AND a.sign_out_time IS NOT NULL " +
                "        THEN '已签退'" +
                "        WHEN a.sign_in_time IS NOT NULL AND a.sign_out_time IS NULL THEN '已签到'" +
                "        ELSE '未签到'" +
                "    END AS status," +
                "    a.sign_in_time," +
                "    a.sign_out_time," +
                "    a.date," +
                "    b.id," +
                "    c.cjr_service_type, " +
                "    c.cjr_service_cat, " +
                "    TO_CHAR(CURRENT_DATE, 'YYYY-MM') as month " +
                " FROM " +
                "    cl_dyq b" +
                " LEFT JOIN " +
                "    cl_sign_record a ON b.id = a.dyq_id ";
        switch (month) {
            case "history":
                condition = " AND a.date = CURRENT_DATE - INTERVAL '1 month'" +
                        " LEFT JOIN" +
                        "    cl_apply_xunlian c on b.bus_id = c.id " +
                        " WHERE " +
                        "    b.month = TO_CHAR(CURRENT_DATE - INTERVAL '1 month', 'YYYY-MM')" +
                        "    and b.org_id = :org_id";
                sql = preSql + condition;
                break;
            case "next":
                condition = " AND a.date = CURRENT_DATE + INTERVAL '1 month'" +
                        " LEFT JOIN" +
                        "    cl_apply_xunlian c on b.bus_id = c.id " +
                        " WHERE " +
                        "    b.month = TO_CHAR(CURRENT_DATE + INTERVAL '1 month', 'YYYY-MM')" +
                        "    and b.org_id = :org_id";
                sql = preSql + condition;
                break;
            default:
                condition = " AND a.date = CURRENT_DATE" +
                        " LEFT JOIN" +
                        "    cl_apply_xunlian c on b.bus_id = c.id " +
                        " WHERE " +
                        "    b.month = TO_CHAR(CURRENT_DATE, 'YYYY-MM')" +
                        "    and b.org_id = :org_id";
                sql = preSql + condition;
                break;
        }

        return sql;
    }
}
